<template>
  <el-container style="height: 100%">
    <el-aside
      style="width: 209px; display: flex; flex-direction: column; height: 100%"
      :span="2"
    >
      <h5 class="title">
        <i class="el-icon-dish-1"></i>
        <span>甜品小屋商家中心</span>
      </h5>
      <el-menu
        router
        unique-opened
        style="height: 100%"
        :default-active="curselected"
        background-color="#B2B5DB"
        text-color="#fff"
        active-text-color="#ffd04b"
        @select="handleSelect"
      >
        <div v-for="item in getrolelist" :key="item.index">
          <!-- 可折叠 -->
          <el-submenu v-if="item.children" :index="item.index">
            <!-- 一级标题名称 -->
            <template slot="title">
              <i style="color: #f0bab4" :class="item.i"></i>
              <span>{{ item.name }}</span>
            </template>

            <!-- 儿子们 -->
            <el-menu-item
              v-for="child in item.children"
              :key="child.index"
              :index="child.index"
              >{{ child.name }}</el-menu-item
            >
          </el-submenu>

          <!-- 不可折叠 -->
          <el-menu-item v-else :index="item.index">
            <i style="color: #f0bab4" :class="item.i"></i>
            <span slot="title">{{ item.name }}</span>
          </el-menu-item>
        </div>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <div style="display: flex; justify-content: space-between">
          <el-breadcrumb style="line-height: 60px" separator="/">
            <el-breadcrumb-item v-for="item in breadlist" :key="item">{{
              item
            }}</el-breadcrumb-item>
          </el-breadcrumb>
          <div style="display: flex">
            <p style="line-height: 60px">{{ name }}</p>
            <div @click="clickAvatar">
              <el-avatar style="margin-left: 10px; margin-top: 8px" :src="img">
              </el-avatar>
            </div>
          </div>
        </div>
      </el-header>
      <el-main style="background-color: #f0bab4; height: 100%">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { checktoken, accountinfo } from "@/apis/users";
import { bus } from "@/utils/utils";

export default {
  data() {
    return {
      name: "",
      img: "", //头像
      curselected: "/home/main", //默认选中首页
      breadlist: [], //面包屑数组
      treeList: [
        {
          index: "/home/main",
          i: "el-icon-s-home",
          name: "后台首页",
          role: ["super", "normal"],
          breadlist: ["首页"], //面包屑对应的名字
        },
        {
          index: "/home/order",
          i: "el-icon-document",
          name: "订单管理",
          role: ["super", "normal"],
          breadlist: ["订单管理"],
        },
        {
          index: "3",
          i: "el-icon-edit-outline",
          name: "商品管理",
          role: ["super", "normal"],
          children: [
            {
              index: "/home/goodslist",
              name: "商品列表",
              breadlist: ["商品管理", "商品列表"],
            },
            {
              index: "/home/goodscategory",
              name: "商品分类",
              breadlist: ["商品管理", "商品分类"],
            },
            {
              index: "/home/AddGoods",
              name: "商品添加",
              breadlist: ["商品管理", "商品添加"],
            },
          ],
        },
        {
          index: "/home/StoreManagement",
          i: "el-icon-shopping-bag-1",
          name: "店铺管理",
          role: ["super"],
          breadlist: ["店铺管理"],
        },
        {
          index: "5",
          i: "el-icon-user-solid",
          name: "账号管理",
          role: ["super"],
          children: [
            {
              index: "/home/accountlist",
              name: "账号列表",
              breadlist: ["账号管理", "账号列表"],
            },
            {
              index: "/home/Addaccount",
              name: "添加账号",
              breadlist: ["账号管理", "添加账号"],
            },
            {
              index: "/home/accountpwd",
              name: "修改密码",
              breadlist: ["账号管理", "修改密码"],
            },
          ],
        },
        {
          index: "6",
          i: "el-icon-picture-outline-round",
          name: "销售统计",
          role: ["super"],
          children: [
            {
              index: "/home/CommodityStats",
              name: "商品统计",
              breadlist: ["销售统计", "商品统计"],
            },
            {
              index: "/home/orderStats",
              name: "订单统计",
              breadlist: ["销售统计", "订单统计"],
            },
          ],
        },
      ],
    };
  },
  created() {
    this.curselected = location.hash.replace("#", ""); //设置默认路由
    this.handleSelect(this.curselected); //设置默认面包屑

    //验证token,判断用户是否还在登录状态，如果在，则显示用户名。 否则显示请登录
    checktoken({ token: localStorage.token }).then((res) => {
      if (res.data.code === 0) {
        this.name = "欢迎您:" + localStorage.acc;
      } else this.name = "请登录";
    });

    //接电话
    bus.$on("refreshHeader", () => {
      this.getuserInfo();
    });

    this.getuserInfo();
  },

  computed: {
    //根据权限角色，获取新的树数组
    getrolelist() {
      return this.treeList.filter((obj) =>
        obj.role.includes(localStorage.role)
      );
    },
  },

  methods: {
    //获取用户信息，渲染头像
    getuserInfo() {
      accountinfo({
        id: localStorage.id,
      }).then((res) => {
        this.img = res.data.accountInfo.imgUrl;
      });
    },

    clickAvatar() {
      this.$router.push("/home/userinfo");
    },

    handleSelect(index) {
      // console.log(index);
      for (let item of this.treeList) {
        //如果有2级节点，就循环2级节点
        if (item.children) {
          //循环二级节点
          for (let child of item.children) {
            if (child.index == index) this.breadlist = child.breadlist;
          }
        } else {
          //一级直接判断
          if (index == item.index) this.breadlist = item.breadlist;
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  width: 209px;
  height: 50px;
  padding-top: 5px;
  line-height: 40px;
  font-size: 16px;
  font-weight: 400;
  text-align: center;
  color: #fff;
  background-color: #b2b5db;
  .el-icon-dish-1 {
    width: 40px;
    height: 40px;
    margin-right: 3px;
    border-radius: 50%;
    line-height: 40px;
    background-color: #f0bab4;
    color: #000;
  }
}
</style>